//
//  PywDatePickerView.m
//  PywSdk2.2_Test
//
//  Created by smallwave on 16/8/13.
//  Copyright © 2016年 yyft. All rights reserved.
//

#import "PywDatePickerView.h"
#import "PywMasonry.h"

@interface PywDatePickerView ()

@property (nonatomic, strong) UILabel       *propmtLabel;   // 提示信息
@property (nonatomic, strong) UIDatePicker  *datePicker;    // 日期选择器
@property (nonatomic, strong) UIButton      *cancelBtn;     // 取消按钮
@property (nonatomic, strong) UIButton      *commitBtn;     // 确定按钮

@end

@implementation PywDatePickerView

#pragma mark - override
- (instancetype)initWithDelegate:(id<PywDatePickerViewDelegate>)delegate{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        
        [self setUp];
    }
    return self;
}
#pragma mark - prepareUI
- (void)setUp{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.layer.borderWidth = 1;
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.propmtLabel];
    [self addSubview:self.datePicker];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.commitBtn];
    
    __weak typeof(self) wSelf = self;
    [self.propmtLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.left.right.equalTo(wSelf);
        make.width.equalTo(wSelf.mas_width);
        make.height.equalTo(@40);
    }];
    [self.datePicker mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.propmtLabel.mas_bottom);
        make.width.equalTo(wSelf.propmtLabel.mas_width);
    }];
    [self.cancelBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.equalTo(wSelf.mas_left);
        make.top.equalTo(wSelf.datePicker.mas_bottom);
        make.width.equalTo(wSelf.mas_width).multipliedBy(0.5);
        make.height.equalTo(@40);
        make.bottom.equalTo(wSelf.mas_bottom);
    }];
    [self.commitBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.bottom.equalTo(wSelf.cancelBtn);
        make.left.equalTo(wSelf.cancelBtn.mas_right).offset(-1);
        make.right.equalTo(wSelf.mas_right);
    }];
    
}
#pragma mark - Action
// 确定操作
- (void)commitAction{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(datePickerView:didCommitWithSelectDate:)]) {
        [self.delegate datePickerView:self didCommitWithSelectDate:[self getDateString]];
    }
}
// 取消操作
- (void)cancelAction{
    [self hidden];
}

- (NSString *)getDateString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *date = [dateFormatter stringFromDate:[self.datePicker date]];
    
    return date;
}
// 隐藏
- (void)hidden{
     [self removeFromSuperview];
}

#pragma mark - Lazy
//提示信息
- (UILabel *)propmtLabel{
    if (!_propmtLabel) {
        _propmtLabel = [[UILabel alloc] init];
        _propmtLabel.backgroundColor = [UIColor blueColor];
        _propmtLabel.textColor = [UIColor whiteColor];
        _propmtLabel.numberOfLines = 1;
        _propmtLabel.textAlignment = NSTextAlignmentCenter;
        _propmtLabel.font = [UIFont systemFontOfSize:16];
        _propmtLabel.text = @"请选择日期";
    }
    return _propmtLabel;
}
//日期选择器
- (UIDatePicker *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        _datePicker.backgroundColor = [UIColor clearColor];
        
        [_datePicker setDatePickerMode:UIDatePickerModeDate];
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        _datePicker.locale = locale;
        NSDateFormatter *formatter_minDate = [[NSDateFormatter alloc] init];
        [formatter_minDate setDateFormat:@"yyyy-MM-dd"];
        
        NSDate *minDate = [formatter_minDate dateFromString:@"1900-01-01"];
        formatter_minDate = nil;
        [_datePicker setMinimumDate:minDate];
    }
    return _datePicker;
}
//取消按钮
- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelBtn.backgroundColor = [UIColor whiteColor];
        _cancelBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _cancelBtn.layer.borderWidth = 1;
        
        [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
//确定按钮
- (UIButton *)commitBtn{
    if (!_commitBtn) {
        _commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _commitBtn.backgroundColor = RGB(252, 177, 14, 1);
        _commitBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _commitBtn.layer.borderWidth = 1;
        
        [_commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_commitBtn setTitle:@"确定" forState:UIControlStateNormal];
        
        [_commitBtn addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commitBtn;
}

@end
